<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/22
 * Time: 9:53
 */
namespace app\admin\model;
use think\Model;
use think\Db;
class Wholetype extends Model
{
	//无限极分类筛选调用
	 public function GetTypeAll($TableName,$pid,$ppid=0,$keyword=''){
	 // 1 永远读取顶级
		 if(isset($ppid) && intval($ppid)>0){
			$Listtype = $this->GetSeatTypeList($TableName,$ppid,$keyword);
		 }else{
			$Listtype = $this->GetSeatTypeList($TableName,0,$keyword);
		 }
        $all_cate[] = $Listtype;
        // 2 读取 pid 并移除空项
        $tmp = explode(',', $pid);
        $pid_arr = [];
		if(isset($ppid) && intval($ppid)>0){
			unset($tmp[0]);
		 }
        foreach($tmp as $v) {
            if (!empty($v)) $pid_arr[] = $v;
        }
        // 3 读取指定分类下的子项
        if (!empty($pid_arr)) {
            $tmp_cate_list = $this->GetSeatTypeListzi($TableName, $pid_arr);
            // 4 分组这些子项
            foreach ($tmp_cate_list as $v) {
                $all_cate[$v['TypePID']][] = $v;
            }
        }
		//dump($all_cate);
		return $all_cate;
	 }
	public function GetSeatTypeList($TableName,$ParentID=0,$keyword='')
    {
        $Where=array();
        $Where['TypeState']='1';
		if($keyword){
			 $Where['TypeName']=['like','%'.$keyword.'%'];
			 $Where[]=['exp',Db::raw("FIND_IN_SET(".intval($ParentID).",TypePath)")];
		}else{
			 $Where['TypePID']=$ParentID;
		}
		$return=db::name($TableName)->field('TypeID,TypeName,TypePath,TypeNode,TypePID')->where($Where)->order('TypeSort Asc,TypeTime Asc')->select();
		//echo db::name($TableName)->getlastsql();
		return $return;
    }
	 //获取局部“子”分类数据
    public function GetSeatTypeListzi($TableName,$ParentID=0)
    {
		if(empty($ParentID)) return false;
        $Where=array();
        $Where['TypePID']=['in',$ParentID];
        $Where['TypeState']='1';
        $result= db::name($TableName)->field('TypeID,TypeName,TypePath,TypeNode,TypePID')->where($Where)->order('TypeSort Asc,TypeTime Asc')->select();
		return $result;
    }
	public function GetSeatTypeLimit($TableName,$ParentID=0,$limit)
    {
        $Where=array();
        $Where['TypePID']=$ParentID;
        $Where['TypeState']='1';
		return	db::name($TableName)->field('TypeID,TypeName,TypePath,TypeNode,TypePID')->where($Where)->limit($limit)->order('TypeSort Asc,TypeID Asc')->select();
    }
    //获取全局分类名称
    public function GetWholeTypeName($TypeID){
        $Where=array();
        $Where['TypeID']=$TypeID;
        $Where['TypeState']=1;
        $Info=$this->field('TypeName')->where($Where)->find();
        return $Info['TypeName'];
    }
	public function GetWholeSeat($WholeSeat){
        if(empty($WholeSeat)) return false;
        $WholeSeat=explode(',',$WholeSeat);
        $Where=array();
		$str=array();
		foreach($WholeSeat as $v)
		{
			 $Where['TypeID']=$v;
			 $str[]=$this->field('TypeName')->where($Where)->order('TypeID Asc')->value('TypeName');
		}
        return implode('->',$str);
    }
	 //获指定的id列表据
    public function GetSeatTypeListin($TableName,$ID=0)
    {
		if(empty($ID)) return false;
        $Where=array();
        $Where['TypeID']=['in',$ID];
        $Where['TypeState']='1';
        return db::name($TableName)->field('TypeID,TypeName,TypePath,TypeNode,TypePID')->where($Where)->order('TypeSort Asc,TypeID Asc')->select();
    }
	//获指定的id的层级
    public function GetTypePathval($ID)
    {
		if(empty($ID)) return false;
        $Where=array();
        $Where['TypeID']=$ID;
        $Where['TypeState']='1';
        return $this->field('TypeID,TypePath')->where($Where)->value('TypePath');
    }
	 //获指定的id列表据
    public function getSelect()
    {
        $Where=array();
        $Where['TypeState']='1';
		$Where['TypePID']='0';
        return $this->field('TypeID,TypeName,TypePath,TypeNode,TypePID')->where($Where)->order('TypeSort Asc,TypeID Asc')->select();
    }
}